<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <!-- <a-col :span="24">
            <a-form-model-item label="门店" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shopId">
              <a-select type="list_multi" v-model="model.shopId" placeholder="请选择门店" >
                <a-select-option v-for="(i) in shopList" :value="i.id" :key="i.id">
                  {{i.name}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="list" v-model="model.status" dictCode="shop_category_status" placeholder="请选择状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="序列" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sequence">
              <a-input-number v-model="model.sequence" placeholder="请输入序列" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { mapGetters } from 'vuex'

  export default {
    name: 'ShopCategoryForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        // shopList: [],
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           lockVersion: [
              { required: true, message: '请输入版本!'},
           ],
           delFlag: [
              { required: true, message: '请输入删除标记!'},
           ],
           shopId: [
              { required: true, message: '请输入门店id!'},
           ],
           name: [
              { required: true, message: '请输入名称!'},
           ],
           status: [
              { required: true, message: '请输入状态0:激活 1:冻结!'},
           ],
           sequence: [
              { required: false},
              { pattern: /^-?\d+$/, message: '请输入整数!'},
           ],
           goodsQty: [
              { required: false},
              { pattern: /^-?\d+$/, message: '请输入整数!'},
           ],
        },
        url: {
          add: "/shop/shopCategory/add",
          edit: "/shop/shopCategory/edit",
          queryById: "/shop/shopCategory/queryById",
          // getShopListUrl: '/shop/shopBasic/selectShopList'
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
      ...mapGetters([
        "userInfoSite",
        "userInfoMerchant",
        "userInfoCurrentShop",
      ]),
    },
    created () {
      // this.getShopListUrl();
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      // getShopListUrl(){
      //   getAction(this.url.getShopListUrl).then(res => {
      //     if (res.success) this.shopList = res.result
      //   })
      // },
      add () {
        this.$emit('handleChangeThreeElements', {
          site: this.userInfoSite ? this.userInfoSite['name'] : null,
          merchant: this.userInfoMerchant ? this.userInfoMerchant['name'] : null,
          shop: this.userInfoCurrentShop ? this.userInfoCurrentShop['name'] : null
        })
        this.visible = true;
      },
      edit (record) {
        this.$emit('handleChangeThreeElements', {
          site: record['siteId_dictText'],
          merchant: record['merchantId_dictText'],
          shop: record['shopId_dictText']
        })
        // this.getShopListUrl();
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>